package com.asiainfo.indb.control.db.pool;

import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * 同一线程不重复获取连接，影响性能开销
 *
 * @author zym
 * @date 2019-10-21 14:23
 * @deprecated 废弃，不应该把连接的控制放到连接底层来，应该在业务层面
 */
public class ThreadLocalJdbcPool extends JdbcPool {

    /**
     * 线程缓存
     */
    private ThreadLocal<Connection> connectionThreadLocal = ThreadLocal.withInitial(() -> {
        try {
            return super.getConnection();
        } catch (SQLException e) {
            // ignore
        }

        return null;
    });

    @Override
    public Connection getConnection() {
        Connection connection = connectionThreadLocal.get();
        if (connection == null) {
            return null;
        }

        return new LogicConnection(connection);
    }

    /**
     * 逻辑连接
     */
    private class LogicConnection implements Connection {

        private Connection physic;

        private LogicConnection(Connection physic) {
            this.physic = physic;
        }

        @Override
        public Statement createStatement() throws SQLException {
            return physic.createStatement();
        }

        @Override
        public PreparedStatement prepareStatement(String sql) throws SQLException {
            return physic.prepareStatement(sql);
        }

        @Override
        public CallableStatement prepareCall(String sql) throws SQLException {
            return physic.prepareCall(sql);
        }

        @Override
        public String nativeSQL(String sql) throws SQLException {
            return physic.nativeSQL(sql);
        }

        @Override
        public boolean getAutoCommit() throws SQLException {
            return physic.getAutoCommit();
        }

        @Override
        public void setAutoCommit(boolean autoCommit) throws SQLException {
            physic.setAutoCommit(autoCommit);
        }

        @Override
        public void commit() throws SQLException {
            physic.commit();
        }

        @Override
        public void rollback() throws SQLException {
            physic.rollback();
        }

        @Override
        public void close() throws SQLException {
            connectionThreadLocal.remove();

            physic.commit();
            physic.close();
        }

        @Override
        public boolean isClosed() throws SQLException {
            return physic.isClosed();
        }

        @Override
        public DatabaseMetaData getMetaData() throws SQLException {
            return physic.getMetaData();
        }

        @Override
        public boolean isReadOnly() throws SQLException {
            return physic.isReadOnly();
        }

        @Override
        public void setReadOnly(boolean readOnly) throws SQLException {
            physic.setReadOnly(readOnly);
        }

        @Override
        public String getCatalog() throws SQLException {
            return physic.getCatalog();
        }

        @Override
        public void setCatalog(String catalog) throws SQLException {
            physic.setCatalog(catalog);
        }

        @Override
        public int getTransactionIsolation() throws SQLException {
            return physic.getTransactionIsolation();
        }

        @Override
        public void setTransactionIsolation(int level) throws SQLException {
            physic.setTransactionIsolation(level);
        }

        @Override
        public SQLWarning getWarnings() throws SQLException {
            return physic.getWarnings();
        }

        @Override
        public void clearWarnings() throws SQLException {
            physic.clearWarnings();
        }

        @Override
        public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
            return physic.createStatement(resultSetType, resultSetConcurrency);
        }

        @Override
        public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
            return physic.prepareStatement(sql, resultSetType, resultSetConcurrency);
        }

        @Override
        public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
            return physic.prepareCall(sql, resultSetType, resultSetConcurrency);
        }

        @Override
        public Map<String, Class<?>> getTypeMap() throws SQLException {
            return physic.getTypeMap();
        }

        @Override
        public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
            physic.setTypeMap(map);
        }

        @Override
        public int getHoldability() throws SQLException {
            return physic.getHoldability();
        }

        @Override
        public void setHoldability(int holdability) throws SQLException {
            physic.setHoldability(holdability);
        }

        @Override
        public Savepoint setSavepoint() throws SQLException {
            return physic.setSavepoint();
        }

        @Override
        public Savepoint setSavepoint(String name) throws SQLException {
            return physic.setSavepoint(name);
        }

        @Override
        public void rollback(Savepoint savepoint) throws SQLException {
            physic.rollback(savepoint);
        }

        @Override
        public void releaseSavepoint(Savepoint savepoint) throws SQLException {
            physic.releaseSavepoint(savepoint);
        }

        @Override
        public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
            return physic.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
        }

        @Override
        public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
            return physic.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
        }

        @Override
        public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
            return physic.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
        }

        @Override
        public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
            return physic.prepareStatement(sql, autoGeneratedKeys);
        }

        @Override
        public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
            return physic.prepareStatement(sql, columnIndexes);
        }

        @Override
        public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
            return physic.prepareStatement(sql, columnNames);
        }

        @Override
        public Clob createClob() throws SQLException {
            return physic.createClob();
        }

        @Override
        public Blob createBlob() throws SQLException {
            return physic.createBlob();
        }

        @Override
        public NClob createNClob() throws SQLException {
            return physic.createNClob();
        }

        @Override
        public SQLXML createSQLXML() throws SQLException {
            return physic.createSQLXML();
        }

        @Override
        public boolean isValid(int timeout) throws SQLException {
            return physic.isValid(timeout);
        }

        @Override
        public void setClientInfo(String name, String value) throws SQLClientInfoException {
            physic.setClientInfo(name, value);
        }

        @Override
        public String getClientInfo(String name) throws SQLException {
            return physic.getClientInfo(name);
        }

        @Override
        public Properties getClientInfo() throws SQLException {
            return physic.getClientInfo();
        }

        @Override
        public void setClientInfo(Properties properties) throws SQLClientInfoException {
            physic.setClientInfo(properties);
        }

        @Override
        public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
            return physic.createArrayOf(typeName, elements);
        }

        @Override
        public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
            return physic.createStruct(typeName, attributes);
        }

        @Override
        public String getSchema() throws SQLException {
            return physic.getSchema();
        }

        @Override
        public void setSchema(String schema) throws SQLException {
            physic.setSchema(schema);
        }

        @Override
        public void abort(Executor executor) throws SQLException {
            physic.abort(executor);
        }

        @Override
        public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
            physic.setNetworkTimeout(executor, milliseconds);
        }

        @Override
        public int getNetworkTimeout() throws SQLException {
            return physic.getNetworkTimeout();
        }

        @Override
        public <T> T unwrap(Class<T> iface) throws SQLException {
            return physic.unwrap(iface);
        }

        @Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
            return physic.isWrapperFor(iface);
        }
    }
}